/**
  ******************************************************************************
  * @file   :   main.c
  * @version:
  * @author :
  * @brief  :
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright(c) . BLUEX Microelectronics.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */

/* includes ------------------------------------------------------------------*/
#include "app.h"
#include "bxd_uart.h"
#include "bxd_gpio.h"

/* private define ------------------------------------------------------------*/
//#define TEST_PIN 16
/* private typedef -----------------------------------------------------------*/

/* private variables ---------------------------------------------------------*/
bool start_send_data;
/* exported variables --------------------------------------------------------*/
volatile u32 sys_tick = 0;
/*============================= private function =============================*/

/*========================= end of private function ==========================*/

/*============================= exported function ============================*/

void SysTick_init( void )
{
    SysTick->LOAD  = ( u32 )( MAIN_CLOCK / 8000  - 1UL );                   /* set reload register */  //1us
    //NVIC_SetPriority ( SysTick_IRQn, ( 1UL << __NVIC_PRIO_BITS ) - 1UL ); /* set Priority for Systick Interrupt */
    NVIC_SetPriority ( SysTick_IRQn, ( 1UL << __NVIC_PRIO_BITS ) - 3UL );
    SysTick->VAL   = 0UL;                                             /* Load the SysTick Counter Value */
    SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
                     SysTick_CTRL_TICKINT_Msk   |
                     SysTick_CTRL_ENABLE_Msk;                         /* Enable SysTick IRQ and SysTick Timer */

    NVIC_ClearPendingIRQ( SysTick_IRQn );
    NVIC_EnableIRQ( SysTick_IRQn );
    __enable_irq();
    NVIC_DisableIRQ( BLE_LP_IRQn );
    NVIC_DisableIRQ( BLE_MAC_IRQn );
}

extern uint32_t i2s_fetch_32bit_64clk(void);
extern void s16toa10( s16 iv, u8 *sp, u32 *sl );
/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
void SysTick_Handler( void )
{
    #ifdef TEST_PIN
    BX_GPIOA->OD |= ( 0x01UL << TEST_PIN );
    #endif

//  make scope
#if 0
    sys_tick++;
	//LOG_RAW("%d\n",sys_tick);
#else
    sys_tick = i2s_fetch_32bit_64clk();
    //u32 port_value = 0;
    //bxd_gpio_read( BX_GPIOA, &port_value);
    //sys_tick = port_value;
#endif
    start_send_data = true;
//  make end

    #ifdef TEST_PIN
    BX_GPIOA->OD &= ~( 0x01UL << TEST_PIN );
    #endif
}
/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
u32 GetSysTick( void )
{
    return sys_tick;
}



/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
void app_init( void )
{
    bxd_gpio_open( BX_GPIOA );
    //BX_PER->CLKG1 |= PER_CLKG1_SET_GPIO;
    #ifdef TEST_PIN
    BX_GPIOA->DIR |= ( 0x01UL << TEST_PIN );
    BX_GPIOA->OD &= ~( 0x01UL << TEST_PIN );
    #endif

    BX_GPIOA->DIR |= ( 0x01UL << 4 );//clk set out
    BX_GPIOA->OD &= ~( 0x01UL << 4 );//clk init low
    
    BX_GPIOA->DIR &= ~( 0x01UL << 5 );//data set in
    BX_AWO->GPIOIS |= ( 0x01UL << 5 );
    BX_AWO->GPIOIE |= ( 0x01UL << 5 );
    
    BX_GPIOA->DIR |= ( 0x01UL << 6 );//ws set out
    BX_GPIOA->OD |= ( 0x01UL << 6 );//ws init high

    //systick init
    SysTick_init();
    
    //uart init
    bxd_uart_open(BX_UART1);
    bxd_uart_set_tx_pin(BX_UART1,12);
    bxd_uart_set_rx_pin(BX_UART1,13);
    bxd_uart_set_data_bit( BX_UART1, BX_UART_DB_8B );
    //bxd_uart_set_speed( BX_UART1, BX_UART_BD_921600 );
    bxd_uart_set_speed( BX_UART1, BX_UART_BD_1000000 );
    bxd_uart_set_stop_bit( BX_UART1, BX_UART_SB_ONE );
    bxd_uart_set_parity( BX_UART1, BX_UART_PARITY_NONE );
    enfifo(BX_UART1);
    bxd_uart_disable_intr(BX_UART1);

//    u8 strp[8]={'-','3','2','7','6','5','\n'};
//    u32 strl=7;
//    BX_GPIOA->OD |= ( 0x01UL << TEST_PIN );
//    bxd_uart_write2(BX_UART1,strp,strl);
//    BX_GPIOA->OD &= ~( 0x01UL << TEST_PIN );

    start_send_data = false;
}

u8 strp[8];
u32 strl;

void app_main(void)
{
    if ( start_send_data )
    {
        start_send_data = false;
        
        strl=0;
        s16 intval = sys_tick;
        s16toa10(intval,strp,&strl);
        strp[strl] = ',';++strl;
//        strp[strl] = 0;
//        LOG_RAW("%s\n",strp);
        bxd_uart_write2(BX_UART1,strp,strl);

    }
}
/*========================= end of exported function =========================*/


/*============================= import function ==============================*/

/*=========================== end of import function =========================*/


/*============================ interrupt function ============================*/

/*========================= end of interrupt function ========================*/


/******************** (C) COPYRIGHT BLUEX **********************END OF FILE****/



